function addParameterRow() {

    var tr =
        '<tr class="bm_tr">' +
        '    <th style="width: 100px"> ' +
        '        <select class="changeType" onchange="changeType(this)"> ' +
        '            <option value="TASK_NAME">任务名称</option> ' +
        '            <option value="WEB_SERVICE_URL">地址</option> ' +
        '            <option value="STATUS">运行状态</option> ' +
        '            <option value="CYCLE">执行周期</option> ' +
        '            <option value="PLAN_TIME">计划时间</option> ' +
        '            <option value="PARAMETER">参数</option> ' +
        '            <option value="DESCRIPTION">任务描述</option> ' +
        '            <option value="SO_TIMEOUT">超时时间</option> ' +
        '            <option value="OWNER">环境</option> ' +
        '        </select> ' +
        '    </th> ' +
        '    <td> ' +
        '        <input name="task.taskName" value="" id="task_taskName" style="width:450px;" type="text">' +
        '    </td> ' +
        '    <td>' +
        '        <a href="javascript:void(0);" onclick="delParameterRow(this)">' +
        '            <img src="' + basePath + 'images/jian.gif">' +
        '        </a>' +
        '    </td> ' +
        '</tr>';

    if ($(".bm_tr").length < 9) {
        $("#batch_modify_tbody").append(tr);
    }
}
function delParameterRow(obj) {
    $(obj).parent().parent().remove();
}
function changeType(obj) {

    var parameterType = $(obj).val();
    var paramHtml;

    switch (parameterType) {
        case 'TASK_NAME' :
            paramHtml = '<input id="task_taskName" name="task.taskName" style="width:450px;" type="text"> *';
            break;
        case 'WEB_SERVICE_URL' :
            paramHtml = '<input id="task_webServiceUrl" name="task.webServiceUrl" style="width:450px;" type="text">';
            break;
        case 'STATUS' :
            paramHtml =
                '<select id="task_status" name="task.status"> ' +
                '    <option value="WAIT">等待</option> ' +
                '    <option value="RUN">运行</option> ' +
                '    <option value="END">结束</option> ' +
                '</select>' +
                ' <span style="color: red;">慎用,可能会导致任务重复执行</span>';
            break;
        case 'CYCLE' :
            paramHtml =
                '<select id="task_cycle" name="task.cycle" onchange="changeCycle(this)"> ' +
                '    <option value="SECOND">秒</option> ' +
                '    <option value="MINUTE">分钟</option> ' +
                '    <option value="HOURS">小时</option> ' +
                '    <option value="DAY">天</option> ' +
                '    <option value="WEEK">周</option> ' +
                '    <option value="MONTH">月</option> ' +
                '    <option value="YEAR">年</option> ' +
                '    <option value="CRON">cron</option> ' +
                '</select>' +
                '<span> 周期尺度:<input id="task_cycleDimension" name="task.cycleDimension" style="text-align: right;width: 50px;" type="text"></span>';
            break;
        case 'PLAN_TIME' :
            paramHtml = '<input id="task_planTime" name="task.planTime" onclick="WdatePicker({isShowClear:true,readOnly:true,dateFmt:\'yyyy-MM-dd HH:mm:ss\'})" class="Wdate" readonly="" type="text">';
            break;
        case 'PARAMETER' :
            paramHtml = '<textarea id="task_parameter" name="task.parameter" style="width: 600px;height: 50px;"/> *';
            break;
        case 'DESCRIPTION' :
            paramHtml = '<textarea id="task_description" name="task.description" style="width: 600px;height: 50px;"/>';
            break;
        case 'SO_TIMEOUT' :
            paramHtml = '<input id="task_soTimeout" name="task.soTimeout" style="text-align: right;width:80px;" type="text">';
            break;
        case 'OWNER' :
            paramHtml = '<input id="task_owner" name="task.owner" style="text-align: left;width:120px;" type="text">';
            break;
    }

    $(obj).parent().next().html(paramHtml);

}
function changeCycle(obj) {
    var cycle = $(obj).val();
    var paramHtml = '<span> 周期尺度:<input id="task_cycleDimension" name="task.cycleDimension" style="text-align: right;width: 50px;" type="text"></span>';

    switch (cycle) {
        case 'SECOND' :
            break;
        case 'MINUTE' :
            break;
        case 'HOURS' :
            break;
        case 'DAY' :
            break;
        case 'WEEK' :
            paramHtml =
                '    <input type="checkbox" id="weeks-1" value="SUNDAY" name="weeks">' +
                '    <label class="checkboxLabel" for="weeks-1">周日</label> ' +
                '    <input type="checkbox" id="weeks-2" value="MONDAY" name="weeks"> ' +
                '    <label class="checkboxLabel" for="weeks-2">周一</label> ' +
                '    <input type="checkbox" id="weeks-3" value="TUESDAY" name="weeks"> ' +
                '    <label class="checkboxLabel" for="weeks-3">周二</label> ' +
                '    <input type="checkbox" id="weeks-4" value="WEDNESDAY" name="weeks"> ' +
                '    <label class="checkboxLabel" for="weeks-4">周三</label> ' +
                '    <input type="checkbox" id="weeks-5" value="THURSDAY" name="weeks"> ' +
                '    <label class="checkboxLabel" for="weeks-5">周四</label> ' +
                '    <input type="checkbox" id="weeks-6" value="FRIDAY" name="weeks"> ' +
                '    <label class="checkboxLabel" for="weeks-6">周五</label> ' +
                '    <input type="checkbox" id="weeks-7" value="SATURDAY" name="weeks"> ' +
                '    <label class="checkboxLabel" for="weeks-7">周六</label> ';
            break;
        case 'MONTH' :
            break;
        case 'YEAR' :
            break;
        case 'CRON' :
            paramHtml =
                '<input name="task.cronExpression" value="" id="task_cronExpression" style="text-align: right;width: 150px;text-align: left;" type="text"> ' +
                '<a href="#" onclick="testCron(this)">测试</a> ';
            break;

    }

    $(obj).next().html(paramHtml);

}
function testCron(obj) {
    $.post(
        basePath + "pub/testCron.do",
        {
            "task.cronExpression": $(obj).prev().val()
        },
        function (data) {
            if (data.jsonMap.status == "success") {
                alert(data.jsonMap.info);
            } else {
                alert(data.jsonMap.status);
            }
        }
    );
}
function showBook() {
    if ($("#bookInfo").is(":visible")) {
        $("#bookInfo").hide();
    } else {
        $("#bookInfo").show();
    }
}
function saveSubmit() {
    $.post(
        basePath + "pub/batchModifyTask.do",
        $("#task_batch_modify_form").serialize(),
        function (data) {

            if (data.jsonMap.status == "success") {
                alert("保存任务成功");
                window.parent.refreshWindow()
            } else {
                alert(data.jsonMap.status);
            }
        }
    );
}
$(document).ready(function () {

    $("#batchModifyTaskButton").click(function () {

        var changeTypeStr = "";
        var changeTypeCheck = true;

        $(".changeType").each(function () {
            if (changeTypeStr.indexOf($(this).val()) > -1) {
                changeTypeCheck = false;
            } else {
                changeTypeStr = changeTypeStr + $(this).val() + ",";
            }
        });

        if (!changeTypeCheck) {
            alert("不能有重复的元素");
            return;
        }

        if ($("#task_taskName").length > 0) {
            var task_taskName = $("#task_taskName").val();
            if ($.trim(task_taskName) == "") {
                alert("“任务名称”不能为空");
                return;
            }
        }

       if ($("#task_owner").length > 0) {
            var task_owner = $("#task_owner").val();
            if ($.trim(task_owner) == "") {
                alert("“环境”不能为空");
                return;
            }
        }

        if ($("#task_parameter").length > 0) {
            var task_parameter = $("#task_parameter").val();
            if ($.trim(task_parameter) == "") {
                alert("“参数”不能为空");
                return;
            }
        }

        if ($("#task_status").length > 0) {
            var task_status = $("#task_status").val();
            if ($.trim(task_status) == "") {
                alert("“运行状态”不能为空");
                return;
            }
        }

        if ($("#task_description").length > 0) {
            var task_description = $("#task_description").val();
            if ($.trim(task_description) == "") {
                alert("“任务描述”不能为空");
                return;
            }
        }

        if ($("#task_webServiceUrl").length > 0) {
            var task_webServiceUrl = $("#task_webServiceUrl").val();
            if ($.trim(task_webServiceUrl) == "") {
                alert("“地址”不能为空");
                return;
            }
        }
        if ($("#task_cycle").length > 0) {
            var task_cycle = $("#task_cycle").val();
            if (task_cycle == 'WEEK') {
                var count = $("input[name='weeks']:checkbox:checked").size();
                if (count == 0) {
                    alert("请选择周几执行");
                    return;
                }
            }
        }
        if ($("#task_planTime").length > 0) {
            var task_planTime = $("#task_planTime").val();
            if (task_cycle != 'CRON' && $.trim(task_planTime) == "") {
                alert("“计划时间”不能为空");
                return;
            }
        }
        var type = "^[0-9]*[1-9][0-9]*$";
        var re = new RegExp(type);
        if ($("#task_cycleDimension").length > 0) {
            var cycleDimension = $("#task_cycleDimension");
            var cycleDimensionVal = $.trim($(cycleDimension).val());
            if (cycleDimensionVal.match(re) == null) {
                alert("“周期尺度”应为正整数!");
                $(cycleDimension).val("1");
                $(cycleDimension).focus();
                return;
            }
        }
        if ($("#task_soTimeout").length > 0) {
            var task_soTimeout = $("#task_soTimeout").val();
            if ($.trim(task_soTimeout) != "" && $.trim(task_soTimeout).match(re) == null) {
                alert("“超时时间”应为正整数!");
                return;
            }
        }
        if ($("#task_cycle").length > 0 && $("#task_cycle").val() == 'CRON') {
            $.post(
                basePath + "pub/testCron.do",
                {
                    "task.cronExpression": $("#task_cronExpression").val()
                },
                function (data) {
                    if (data.jsonMap.status == "success") {
                        saveSubmit();
                    } else {
                        alert(data.jsonMap.status);
                    }
                }
            );

        } else {
            saveSubmit();
        }
    });
});